package spec.chenli;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sound.midi.Receiver;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

//@WebServlet(name = "NewsyServlet")
public class NewsyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            // 1、获取浏览器端输入的用户名和密码
            String btitle = request.getParameter("title");
            String bcontent = request.getParameter("content");
            //      注册驱动
            try {
                Class.forName("com.mysql.jdbc.Driver");
                Connection con = null;
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/1613580007_1?characterEncoding=utf8", "root","123456789");
                Statement s = con.createStatement();
                String sql = "insert into t_news values(Null,'"+btitle+"','"+bcontent+"',Null,Null)";
                int m = 0;
                m = s.executeUpdate(sql);
                if (m >0) {
                    out.println("<h1>保存成功</h1>");
                    response.sendRedirect("listnew");
                } else {
                    out.println("<h1>保存失败</h1>");
                    out.print("<a href=\"http://localhost:8080/NewsAdd.html\">重新保存</a>");
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        protected void doGet (HttpServletRequest request, HttpServletResponse response) throws
        ServletException, IOException {
            //设置响应内容类型
            // response.setContentType("text/html");
            //设置逻辑实现
            // PrintWriter out = response.getWriter();
            // out.println("<h1>登录成功</h1>");
        }
    }
